package com.ktjiaoyu.ktmall.servlet;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.service.CategoryService;
import com.ktjiaoyu.ktmall.service.impl.CategoryServiceImpl;
import com.ktjiaoyu.ktmall.utils.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CategoryServlet", urlPatterns = "/api/category/*")
public class CategoryServlet extends HttpServlet {

    private final CategoryService categoryService = new CategoryServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String path = request.getPathInfo();
        String opr = request.getParameter("opr") == null ? "all" : request.getParameter("opr");
        switch (opr) {
            case "update":
                // 新增/修改
                BufferedReader reader = request.getReader();
                StringBuilder sb = new StringBuilder();
                String line;
                while (((line = reader.readLine()) != null)) {
                    sb.append(line);
                }
                Category category = JSON.parseObject(sb.toString(), Category.class);
                boolean success = categoryService.saveOrUpdate(category);
                R r = success ? R.ok() : R.fail();
                out.print(JSON.toJSONString(r));
                out.flush();
                out.close();

                break;
            case "add":
                BufferedReader reader2 = request.getReader();
                StringBuilder sb2 = new StringBuilder();
                String line2;
                while (((line2 = reader2.readLine()) != null)) {
                    sb2.append(line2);
                }
                Category cate = JSON.parseObject(sb2.toString(), Category.class);
                boolean success2 = categoryService.saveOrUpdate(cate);
                R r2 = success2 ? R.ok() : R.fail();
                out.print(JSON.toJSONString(r2));
                out.flush();
                out.close();
                break;
            case "delete":
                Long id = request.getParameter("id") == null ? 0L : Long.valueOf(request.getParameter("id"));

                boolean succe = id != 0 && categoryService.removeById(id);
                R res = succe ? R.ok() : R.fail();
                out.print(JSON.toJSONString(res));
                out.flush();
                out.close();
                break;
            default:
                R result;
                if ("/tree".equals(path)) {
                    List<Category> tree = categoryService.findTree();
                    result = R.ok(tree);
                } else {
                    List<Category> list = categoryService.findAll();
                    result = R.ok(list);
                }
                out.print(JSON.toJSONString(result));
                break;
        }
        out.flush();
        out.close();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
